﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using DIMP_Libraries;
using System.Threading;

namespace DIMP_client
{
   public class Client : ClientServeur
    {
       public clientState State { get; set; }
       public enum clientState{
            init,
            canWrite
       }

       public Client(String ip, Int32 port) : base(ip,port)
       {
           State = clientState.init;
           onMessageReceived += new EventHandler<MessageEventArg>(Client_onMessageReceived);
       }

       public void Ecrire()
       {
           Console.WriteLine("Message ? :");
           String message = Console.ReadLine();

           SendTextMessage unMsg = new SendTextMessage();
           unMsg.Texte = message;

           this.SendMessage(unMsg);
       }

        /*
         * Quand le serveur a envoyé un message
         */
       void Client_onMessageReceived(object sender, MessageEventArg e)
       {
           DIMP_Libraries.IMessage msg = (DIMP_Libraries.IMessage)e.Message;
           Console.WriteLine("Message de type : " + msg.GetType().FullName);

           if (msg.GetType() == typeof(ActionMessage))
           {
               ActionMessage message = (ActionMessage)msg;
               switch (message.Type)
               {
                   case ActionType.Welcome:
                       this.State = clientState.canWrite;
                       break;
                   case ActionType.ServerOff:
                       break;
                   default:
                       break;
               }
           }
           if (msg.GetType() == typeof(SendTextMessage))
           {
               Console.WriteLine(((SendTextMessage)msg).Texte);
           }
       }

      
    }
}
